import { Col, Form, Input, message, Modal, Row, Switch } from 'antd';
import React, { useState } from 'react';

import dictionaryService from '@/api/services/stars/dictionaryService';

import { Dictionaries } from './type';

import { ModalProps } from '#/entity';

export default function DictionaryModal({
  title,
  show,
  onOk,
  onCancel,
  formValue,
}: ModalProps<Dictionaries>) {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
  const onFinish = (values: Dictionaries) => {
    if (formValue.id) {
      const updateForm = {
        ...values,
        status: values.status,
        system: values.system,
      };
      dictionaryService.edit(formValue.id, updateForm).then(
        (res) => {
          if (res > 0) {
            message
              .open({
                content: '修改成功',
                type: 'success',
                duration: 1,
              })
              .then(() => {
                setLoading(false);
                onOk();
              });
          }
        },
        () => setLoading(false),
      );
    } else {
      const createForm = {
        ...values,
        status: values.status,
        system: values.system,
      };
      dictionaryService.add(createForm).then(
        (res) => {
          if (res > 0) {
            message
              .open({
                content: '新增成功',
                type: 'success',
                duration: 1,
              })
              .then(() => {
                setLoading(false);
                onOk();
              });
          }
        },
        () => setLoading(false),
      );
    }
  };

  const modalRender = (dom: React.ReactNode) => (
    <Form
      onFinish={onFinish}
      initialValues={formValue}
      form={form}
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 18 }}
      clearOnDestroy
      layout="horizontal"
      // layout="vertical"
    >
      {dom}
    </Form>
  );
  const required = true;
  return (
    <Modal
      width={500}
      destroyOnHidden
      centered
      title={title}
      open={show}
      onCancel={onCancel}
      confirmLoading={loading}
      modalRender={modalRender}
      okButtonProps={{ autoFocus: true, htmlType: 'submit' }}
    >
      <Row gutter={24}>
        <Col span={24}>
          <Form.Item name="name" label="字典名称" rules={[{ required }]}>
            <Input maxLength={32} showCount placeholder="请输入字典名称" />
          </Form.Item>
        </Col>
        <Col span={24}>
          <Form.Item name="key" label="字典键" rules={[{ required }]}>
            <Input
              maxLength={64}
              showCount
              placeholder="请输入字典键"
              disabled={formValue.system}
            />
          </Form.Item>
        </Col>
        <Col span={24}>
          <Form.Item name="remark" label="字典说明">
            <Input maxLength={64} showCount placeholder="请输入字典说明" />
          </Form.Item>
        </Col>
        <Col span={24}>
          <Form.Item name="status" label="启用状态">
            <Switch />
          </Form.Item>
        </Col>
        <Col span={24}>
          <Form.Item name="system" label="系统内置">
            <Switch />
          </Form.Item>
        </Col>
      </Row>
    </Modal>
  );
}
